import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-02-28
 * Time:21:28
 */
public class Text3 {
        public int totalFruit(int[] fruits) {
            Map<Integer,Integer> hash=new HashMap<Integer,Integer>();//统计窗口内水果的种类
            int ret=0;
            for(int left=0,right=0;right<fruits.length;right++){
                int in=fruits[right];
                hash.put(in,hash.getOrDefault(in,0)+1);//进窗口
                while(hash.size()>2){
                    int out=fruits[left];
                    hash.put(out,hash.get(out)-1);//出窗口
                    if(hash.get(out)==0){
                        hash.remove(out);
                    }
                    left++;
                }
                ret=Math.max(ret,right-left+1);//更新结果
            }
            return ret;
        }
}
